<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>EventSource测试</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">

    if(window.EventSource){

        var eventSource = new EventSource("http://127.0.0.1:10004/eventSource");

        //只要和服务器连接，就会触发open事件
        eventSource.addEventListener("open",function(){
            console.log("open事件","和服务器建立连接");
        });

        //处理服务器响应报文中的load事件
        eventSource.addEventListener("load",function(e){
            console.log("load事件",e.data);
            console.log(e);
        });

        //如果服务器响应报文中没有指明事件，默认触发message事件
        eventSource.addEventListener("message",function(e){
            console.log("message事件",e.data);
            console.log(e);
        });

        //发生错误，则会触发error事件
        eventSource.addEventListener("error",function(e){
            console.log("error事件",e.data);
            console.log(e);
        });
    }
    else{
        console.log("服务器不支持EvenSource对象");
    }

</script>